# 作为模块级全局变量：在一个单独的模块（例如config.py）中定义全局变量，并在需要它的其他模块中导入。
# config.py
from datetime import datetime

import logging

log_file_path = r'./file.log'


def initial_logging_config():
    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s - %(levelname)s - %(message)s')
    # 创建一个FileHandler，用于写入日志文件
    file_handler = logging.FileHandler("compression.log", encoding='utf-8')
    file_handler.setLevel(logging.INFO)
    # 创建一个StreamHandler，用于输出到控制台
    stream_handler = logging.StreamHandler()
    stream_handler.setLevel(logging.INFO)
    # 定义handler的输出格式
    formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
    file_handler.setFormatter(formatter)
    stream_handler.setFormatter(formatter)
    # 给logger添加handler
    logging.getLogger('').addHandler(file_handler)
    logging.getLogger('').addHandler(stream_handler)


def print_console_and_write_log(logging_info):
    current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    logging_info = f"[{current_time}] " + logging_info
    print(logging_info)
    with open(log_file_path, 'a') as log_file:
        log_file.write(logging_info + "\n")
